package com.locationtracker.plus.app.util

import android.text.TextUtils
import com.google.gson.Gson
import com.locationtracker.plus.data.model.bean.RouteLocation
import com.locationtracker.plus.data.model.bean.UserInfo
import com.tencent.mmkv.MMKV

object CacheUtil {
    /**
     * 获取保存的账户信息
     */
    fun getUser(): UserInfo? {
        val kv = MMKV.mmkvWithID("app")
        val userStr = kv.decodeString("user")
        return if (TextUtils.isEmpty(userStr)) {
            null
        } else {
            Gson().fromJson(userStr, UserInfo::class.java)
        }
    }

    /**
     * 设置账户信息
     */
    fun setUser(userResponse: UserInfo?) {
        val kv = MMKV.mmkvWithID("app")
        if (userResponse == null) {
            kv.encode("user", "")
            setIsLogin(false)
        } else {
            kv.encode("user", Gson().toJson(userResponse))
            setLastLoginPhone(userResponse.phone)
            setIsLogin(true)
        }
    }

    /**
     * 是否已经登录
     */
    fun isLogin(): Boolean {
        val kv = MMKV.mmkvWithID("app")
        return kv.decodeBool("login", false)
    }

    /**
     * 设置是否已经登录
     */
    fun setIsLogin(isLogin: Boolean) {
        val kv = MMKV.mmkvWithID("app")
        kv.encode("login", isLogin)
    }

    fun getToken(): String {
        val kv = MMKV.mmkvWithID("app")
        return kv.decodeString("token", "")
    }

    fun setToken(token: String?) {
        val kv = MMKV.mmkvWithID("app")
        if (token == null) {
            kv.encode("token", "")
            setIsLogin(false)
        } else {
            kv.encode("token", token)
            setIsLogin(true)
        }
    }

    /**
     * 是否是第一次登陆
     */
    fun isFirst(): Boolean {
        val kv = MMKV.mmkvWithID("app")
        return kv.decodeBool("first", true)
    }

    /**
     * 是否是第一次登陆
     */
    fun setFirst(first: Boolean): Boolean {
        val kv = MMKV.mmkvWithID("app")
        return kv.encode("first", first)
    }

    fun getLastLoginPhone(): String {
        val kv = MMKV.mmkvWithID("app")
        return kv.decodeString("phone", "")
    }

    private fun setLastLoginPhone(phone: String?) {
        val kv = MMKV.mmkvWithID("app")
        if (phone == null) {
            kv.encode("phone", "")
        } else {
            kv.encode("phone", phone)
        }
    }

    fun neverShowLocationDialog(): Boolean {
        val kv = MMKV.mmkvWithID("app")
        return kv.decodeBool("locationDialog", true)
    }

    fun neverShowLocationDialog(never: Boolean): Boolean {
        val kv = MMKV.mmkvWithID("app")
        return kv.encode("locationDialog", never)
    }

    fun getLastLocation(): RouteLocation? {
        val kv = MMKV.mmkvWithID("app")
        val location = kv.decodeString("lastLocation")
        return if (TextUtils.isEmpty(location)) {
            null
        } else {
            Gson().fromJson(location, RouteLocation::class.java)
        }
    }

    fun setLastLocation(location: RouteLocation) {
        val kv = MMKV.mmkvWithID("app")
        kv.encode("lastLocation", Gson().toJson(location))
    }

    /**
     * 是否已经同意协议
     */
    fun isAgreement(): Boolean {
        val kv = MMKV.mmkvWithID("app")
        return kv.decodeBool("agreement", false)
    }

    /**
     * 设置是否已经同意协议
     */
    fun setIsAgreement(isAgree: Boolean) {
        val kv = MMKV.mmkvWithID("app")
        kv.encode("agreement", isAgree)
    }

    fun getForeverId(): String {
        val kv = MMKV.mmkvWithID("app")
        return kv.decodeString("forever", "")
    }

    fun setForeverId(id: String?) {
        val kv = MMKV.mmkvWithID("app")
        if (id == null) {
            kv.encode("forever", "")
        } else {
            kv.encode("forever", id)
        }
    }

    fun getLastAdTime(): Long {
        val kv = MMKV.mmkvWithID("app")
        return kv.decodeLong("lastAdTime")
    }

    fun setLastAdTime(time: Long) {
        val kv = MMKV.mmkvWithID("app")
        kv.encode("lastAdTime", time)
    }
}